package com.my.zl.console.service.visualEdit.impl;

import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.my.zl.common.dto.ReqDTO;
import com.my.zl.common.dto.advancedQuery.PageQueryDTO;
import com.my.zl.common.enums.resp.GlobalEnum;
import com.my.zl.common.exception.BizException;
import com.my.zl.common.utils.CopyUtil;
import com.my.zl.console.common.constant.AutocodeC;
import com.my.zl.console.dao.visualEdit.ZlHtmlDesignMapper;
import com.my.zl.console.dto.visualEdit.htmlDesign.ReqHtmlDesignAddDTO;
import com.my.zl.console.dto.visualEdit.htmlDesign.ReqHtmlDesignDelDTO;
import com.my.zl.console.dto.visualEdit.htmlDesign.ReqHtmlDesignDetailDTO;
import com.my.zl.console.dto.visualEdit.htmlDesign.ReqHtmlDesignEditDTO;
import com.my.zl.console.dto.visualEdit.htmlDesign.ReqHtmlDesignQueryDTO;
import com.my.zl.console.dto.visualEdit.htmlDesign.ReqHtmlDesignStateDTO;
import com.my.zl.console.dto.visualEdit.htmlDesign.ResHtmlDesignAddDTO;
import com.my.zl.console.dto.visualEdit.htmlDesign.ResHtmlDesignDelDTO;
import com.my.zl.console.dto.visualEdit.htmlDesign.ResHtmlDesignDetailDTO;
import com.my.zl.console.dto.visualEdit.htmlDesign.ResHtmlDesignEditDTO;
import com.my.zl.console.dto.visualEdit.htmlDesign.ResHtmlDesignQueryDTO;
import com.my.zl.console.dto.visualEdit.htmlDesign.ResHtmlDesignStateDTO;
import com.my.zl.console.dto.visualEdit.htmlDesign.ResHtmlDesignVO2;
import com.my.zl.console.entity.visualEdit.ZlHtmlDesign;
import com.my.zl.console.po.visualEdit.htmlDesign.ZlHtmlDesignVo;
import com.my.zl.console.service.base.BaseManager;
import com.my.zl.console.service.visualEdit.IHtmlDesignService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 在线页面
 * @author ZL
 */
@Service
public class HtmlDesignServiceImpl extends BaseManager implements IHtmlDesignService {

    @Autowired
    private ZlHtmlDesignMapper htmlDesignMapper;

    @Override
    public ResHtmlDesignQueryDTO queryHtmlDesignList(ReqHtmlDesignQueryDTO reqInfo,PageQueryDTO page) throws Exception{
        // TODO Auto-generated method stub
        ResHtmlDesignQueryDTO resDTO = BeanUtil.copyProperties(page, ResHtmlDesignQueryDTO.class);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        
        pageHandle(page);//处理分页数据
        //当效率不行时自行计数，或完全自行分页，先查询总数，如果为0不用再次查询了；开启计数：true是,false否
        //临近第一条SQL查询语句自动分页,如果未消费时(即后面没有触发SQL查询)则需主动调用pageHp.clear()清理
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZlHtmlDesignVo> list = htmlDesignMapper.selectListPage(paramMap);
        resDTO.setData(CopyUtil.copyList(list, ResHtmlDesignVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResHtmlDesignDetailDTO queryHtmlDesignDetail(ReqHtmlDesignDetailDTO reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ResHtmlDesignDetailDTO res = new ResHtmlDesignDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        ZlHtmlDesign view = htmlDesignMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResHtmlDesignAddDTO insertHtmlDesignInfo(ReqDTO<ReqHtmlDesignAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqHtmlDesignAddDTO reqData = reqInfo.getReqData();
        ResHtmlDesignAddDTO res = new ResHtmlDesignAddDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlHtmlDesign record = new ZlHtmlDesign();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setHtmlId(IdUtil.fastSimpleUUID());
        record.setDataStatus(AutocodeC.DataStatus.ENABLE.getCode());//状态
        record.setRandomCode(StrUtil.uuid().substring(0,8));
        
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("htmlId",record.getHtmlId());
        //检查主键是否冲突
        if(htmlDesignMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalEnum.DUPLICATE_PRIMARY_KEY);
        }
        
        htmlDesignMapper.insertSelective(record);
        
        ZlHtmlDesign keyInfo = htmlDesignMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResHtmlDesignEditDTO updateHtmlDesignInfo(ReqDTO<ReqHtmlDesignEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqHtmlDesignEditDTO reqData = reqInfo.getReqData();
        ResHtmlDesignEditDTO res = new ResHtmlDesignEditDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlHtmlDesign record = new ZlHtmlDesign();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        record.setRandomCode(StrUtil.uuid().substring(0,8));
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("htmlId",record.getHtmlId());
        paramMap.put("versionNum", record.getVersionNum());
        paramMap.put("randomCode", record.getRandomCode());
        htmlDesignMapper.updateByMapSelective(record,paramMap);
        
        paramMap.clear();
        paramMap.put("htmlId",record.getHtmlId());
        ZlHtmlDesign keyInfo = htmlDesignMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResHtmlDesignDelDTO deleteHtmlDesignInfo(ReqDTO<ReqHtmlDesignDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqHtmlDesignDelDTO reqData = reqInfo.getReqData();
        ResHtmlDesignDelDTO res = new ResHtmlDesignDelDTO();
        Instant time = getSystemTime().getUtcTime();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        htmlDesignMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResHtmlDesignStateDTO updateHtmlDesignState(ReqDTO<ReqHtmlDesignStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqHtmlDesignStateDTO reqData = reqInfo.getReqData();
        ResHtmlDesignStateDTO res = new ResHtmlDesignStateDTO();
        Instant time = getSystemTime().getUtcTime();
         
        ZlHtmlDesign record = new ZlHtmlDesign();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        htmlDesignMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("htmlId",record.getHtmlId());
        ZlHtmlDesign keyInfo = htmlDesignMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
}